package com.lottery.pay.Utils.wxUtil;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 本类提供对于数据的MD5加解密方法
 * 
 * @author DoveDeng
 * @date 2012-11-23
 */
public class MD5Util {
	public MD5Util() {
	}

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 将字节转换成数字字符串
	 * 
	 * @param b
	 * @return
	 */
	public static String byteToNumString(byte b) {
		int result = b;
		if (result < 0) {
			result = 256 + result;
		}
		return String.valueOf(result);
	}

	/**
	 * 对入参字符串进行MD5加密
	 * 
	 * @param origin
	 * @return
	 */
	public static String encode(String origin) {
		String resultString = null;
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			// There won't these kinds of exception throw, so catch it.
			e.printStackTrace();
			return null;
		}
		byte[] by = md.digest(origin.getBytes());
		resultString = byteArrayToString(by);
		return resultString;
	}

	/**
	 * 对入参字符数组进行MD5加密
	 * 
	 * @param origin
	 * @return
	 */
	public static String encode(byte[] origin) {
		String resultString = null;
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			// There won't these kinds of exception throw, so catch it.
			e.printStackTrace();
			return null;
		}
		byte[] by = md.digest(origin);
		resultString = byteArrayToString(by);
		return resultString;
	}

	private static String byteArrayToString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			// Return 32 bits Hex value.
			resultSb.append(byteToHexString(b[i]));

			// Retirm 32 bits Oct value.
			// resultSb.append(byteToNumString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	/**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
    	text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }
    
    /**
     * @param content
     * @param charset
     * @return
     * @throws SignatureException
     * @throws UnsupportedEncodingException
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }

    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param sign 签名结果
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static boolean verify(String text, String sign, String key, String input_charset) {
    	text = text + key;
    	String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
    	if(mysign.equals(sign)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
}
